// https://card.niconi.co.ni/cardApi/1747


let cardNameListDom = document.createElement("div");
cardNameListDom.style.display = "none";
let cardUrlListDom = document.createElement("div");
cardUrlListDom.style.display = "none";

let doubleCardNameListDom = document.createElement("div");
doubleCardNameListDom.style.display = "none";
let doubleCardUrlListDom = document.createElement("div");
doubleCardUrlListDom.style.display = "none";

let bodyDom = document.getElementsByTagName("body")[0];
bodyDom.appendChild(cardNameListDom);
bodyDom.appendChild(cardUrlListDom);
bodyDom.appendChild(doubleCardNameListDom);
bodyDom.appendChild(doubleCardUrlListDom);

let cardNameHTML = "";
let cardUrlHTML = "";
let doubleCardNameHTML = "";
let doubleCardUrlHTML = "";


let BASE_CARD_URL = "https://card.niconi.co.ni/card/v4nb/";
let BASE_CARD_TRANS_URL = "https://card.niconi.co.ni/asset/";
let BASE_DOUBLE_CARD_URL = "https://card.niconi.co.ni/pair/nb/";
let nameTeamMap = {
    高坂穂乃果:  { name: "高坂穗乃果", team:  "μ's"},
    南ことり:   { name:  "南小鸟", team:  "μ's"},
    園田海未:   { name:  "园田海未", team:  "μ's"},
    絢瀬絵里:   { name:  "绚濑绘里", team:  "μ's"},
    東條希:    { name:   "东条希", team:  "μ's"},
    矢澤にこ:   { name:  "矢泽妮可", team:  "μ's"},
    星空凛:    { name:   "星空凛", team:  "μ's"},
    西木野真姫:  { name: "西木野真姬", team:  "μ's"},
    小泉花陽:   { name:  "小泉花阳", team:  "μ's"},

    高海千歌:   { name:  "高海千歌", team: "Aqours"},
    桜内梨子:   { name:  "樱内梨子", team: "Aqours"},
    渡辺曜:    { name:   "渡边曜", team: "Aqours"},
    津島善子:   { name:  "津岛善子", team: "Aqours"},
    国木田花丸:  { name: "国木田花丸", team: "Aqours"},
    黒澤ルビィ:  { name: "黑泽露比", team: "Aqours"},
    小原鞠莉:   { name:  "小原鞠莉", team: "Aqours"},
    松浦果南:   { name:  "松浦果南", team: "Aqours"},
    黒澤ダイヤ:  { name: "黑泽黛雅", team: "Aqours"},

    優木せつ菜: { name: "优木雪菜", team: "虹团"},
    中須かすみ: { name: "中须霞", team: "虹团"},
    桜坂しずく: { name: "樱坂雫", team: "虹团"},
    天王寺璃奈: { name: "天王寺璃奈", team: "虹团"},
    朝香果林: { name: "朝香果林", team: "虹团"},
    近江彼方: { name: "近江彼方", team: "虹团"},
    "エマ・ヴェルデ": { name: "艾玛·维尔德", team: "虹团"},
    上原歩夢: { name: "上原步梦", team: "虹团"},
    宮下愛: { name: "宫下爱", team: "虹团"},

    鹿角理亞: { name: "鹿角理亚", team: "Saint Snow"},
    鹿角聖良: { name: "鹿角圣良", team: "Saint Snow"},
    綺羅ツバサ: { name: "绮罗翼", team: "A_RISE"},
    優木あんじゅ: { name: "优木杏树", team: "A_RISE"},
    統堂英玲奈: { name: "统堂英玲奈", team: "A_RISE"},
}

let card_type_list = [null, '通常', '活动', '特典', '练习用', 'BOX'];
let rarity_list = [null, "N", "R", "SR", "UR", "SSR"];

let dFlag = false;
let dPreIdolName = "";

for (let i = 2701 ; i <= 2816; i++) {
    getInfo(i)
}

cardNameListDom.innerHTML = cardNameHTML;
cardUrlListDom.innerHTML = cardUrlHTML;
doubleCardNameListDom.innerHTML = doubleCardNameHTML;
doubleCardUrlListDom.innerHTML = doubleCardUrlHTML;
console.log("end ! ")

function getInfo(id) {
    let xml = new XMLHttpRequest();
    xml.open("GET", "https://card.niconi.co.ni/cardApi/" + id, false);
    xml.onreadystatechange = function() {
        if (xml.readyState === 4 && xml.status === 200) {
            let data = JSON.parse(xml.responseText);

            let rarity = rarity_list[data["unit"]["rarity"]];
            if (rarity === "N") {
                return ;
            }

            let newVersion = false;
            // let newVersion = id >= 1930;
            id = "000" + id;
            id = id.substring(id.length - 4);

            let idolName = data["type"]["name"].replace(/ +|　+/g, "");
            let team = "";
            if (data["tags"].length !== 0) {
                team = data["tags"][0]["name"];
            }
            if (team === "虹ヶ咲") {
                team = "虹团";
            }
            if (team === "A-RISE") {
                team = "A_RISE";
            }

            if (nameTeamMap[idolName] !== undefined) {
                team = nameTeamMap[idolName]["team"];
                idolName = nameTeamMap[idolName]["name"];
            } else {
                if (idolName === "μ's" || idolName === "Aqours") {
                    team = idolName;
                }
            }

            let skill = data["skill"]["name"];
            if (skill === undefined) {
                skill = "NULL";
            }
            skill = skill.replaceAll("_", "");
            let collection = data["tag_name"];
            if (collection === null) {
                collection = "NULL";
            }
            collection = collection
                .replace("特典-Aqours　", "")
                .replace("特典-μ's　", "")
                .replace("イベント-", "")
                .replace("特典-", "")
                .replace("BOX-", "")
                .replaceAll("-", "_");
            // 卡牌类型（特典 活动 box）
            let cardType = card_type_list[data["card_type"]];


            // 觉醒立绘
            // let cardIdolizedTransUrl = BASE_CARD_TRANS_URL + data["rank_max_navi_asset"]["unit_navi_asset"];
            // let cardIdolizedTransUrlNew = BASE_CARD_TRANS_URL + data["rank_max_navi_asset"]["unit_navi_asset"].replace("png", "texb").replace("u_", "tx_u_");
            // let cardIdolizedTransId = cardIdolizedTransUrl.substring(cardIdolizedTransUrl.lastIndexOf('/') + 1, cardIdolizedTransUrl.lastIndexOf('.'));
            // let cardIdolizedTransIdNew = cardIdolizedTransUrlNew.substring(cardIdolizedTransUrlNew.lastIndexOf('/') + 1, cardIdolizedTransUrlNew.lastIndexOf('.'));
            // let cardIdolizedTransName = cardIdolizedTransIdNew + "~~~SIF立绘-" + id + "-" + team + "-" + idolName + "-" + cardType + "-" + collection + "-" + rarity + "-觉醒-" + skill;
            // cardUrlHTML += cardIdolizedTransUrlNew + "<br />";
            // cardNameHTML += cardIdolizedTransName + "<br />";
            // console.log(cardIdolizedTransName);
            //
            // // 觉醒卡牌
            // let cardIdolizedId = data["unit"]["rank_max_card_id"];
            // let cardIdolizedUrl;
            // if (newVersion) {
            //     // u_rankup_navi_41002012 改为
            //     // b_rankup_41002012
            //     cardIdolizedId = cardIdolizedTransId.replace("u_rankup_navi", "b_rankup");
            //     cardIdolizedUrl = cardIdolizedTransUrl.replace("u_rankup_navi", "b_rankup");
            // } else {
            //     cardIdolizedUrl = BASE_CARD_URL + cardIdolizedId + ".png";
            // }
            // let cardIdolizedName = cardIdolizedId + "~~~SIF卡牌-" + id + "-" + team + "-" + idolName + "-" + cardType + "-" + collection + "-" + rarity + "-觉醒-" + skill;
            // cardUrlHTML += cardIdolizedUrl + "<br />";
            // cardNameHTML += cardIdolizedName + "<br />";
            // console.log(cardIdolizedName);
            //
            // let cardTransUrl = BASE_CARD_TRANS_URL + data["normal_navi_asset"]["unit_navi_asset"];
            // let cardTransUrlNew = BASE_CARD_TRANS_URL + data["normal_navi_asset"]["unit_navi_asset"].replace("png", "texb").replace("u_", "tx_u_");
            // let cardTransId = cardTransUrl.substring(cardTransUrl.lastIndexOf('/') + 1, cardTransUrl.lastIndexOf('.'));
            // let cardTransIdNew = cardTransUrlNew.substring(cardTransUrlNew.lastIndexOf('/') + 1, cardTransUrlNew.lastIndexOf('.'));
            // // 普通立绘
            // if (data["unit"]["normal_unit_navi_asset_id"] !== data["unit"]["rank_max_unit_navi_asset_id"]) {
            //     let cardIdolizedTransName = cardTransIdNew + "~~~SIF立绘-" + id + "-" + team + "-" + idolName + "-" + cardType + "-" + collection + "-" + rarity + "-普通-" + skill;
            //     cardUrlHTML += cardTransUrlNew + "<br />";
            //     cardNameHTML += cardIdolizedTransName + "<br />";
            //     console.log(cardIdolizedTransName);
            // }
            //
            // // 普通卡牌
            // let cardId = data["unit"]["normal_card_id"];
            // if (cardId !== data["unit"]["rank_max_card_id"]) {
            //     let cardUrl;
            //     if (newVersion) {
            //         cardId = cardTransId.replace("u_normal_navi", "b_normal");
            //         cardUrl = cardTransUrl.replace("u_normal_navi", "b_normal");
            //     } else {
            //         cardUrl = BASE_CARD_URL + cardId + ".png";
            //     }
            //     let cardIdolizedName = cardId + "~~~SIF卡牌-" + id + "-" + team + "-" + idolName + "-" + cardType + "-" + collection + "-" + rarity + "-普通-" + skill;
            //     cardUrlHTML += cardUrl + "<br />";
            //     cardNameHTML += cardIdolizedName + "<br />";
            //     console.log(cardIdolizedName);
            // }


            // 上面为卡牌和立绘，下面为合卡抓取

            // if (data["pair"] !== false && data["pair"]["detail"] !== undefined) {
            //     let leftCardId = data["pair"]["normal"]["left_normal_card_id"];
            //     let rightCardId = data["pair"]["normal"]["right_normal_card_id"];
            //     let leftCardIdolizedId = data["pair"]["normal"]["left_rank_max_card_id"];
            //     let rightCardIdolizedId = data["pair"]["normal"]["right_rank_max_card_id"];
            //
            //     let leftIdolName = idolName;
            //     let rightIdolName = nameTeamMap[data["pair"]['detail']["name"].replace(/ +|　+/g, "")]['name'];
            //     let leftId = id;
            //     let rightId = "000" + data["pair"]["detail"]["unit_number"];
            //     rightId = rightId.substring(rightId.length - 4);
            //
            //     if (leftCardId === data["unit"]["normal_card_id"]) {
            //         let doubleCardName = rightCardId + "~~~SIF合卡-" + leftId + "_" + rightId + "-" + team + "-" + leftIdolName + "_" + rightIdolName
            //             + "-" + cardType + "-" + collection + "-" + rarity + "-普通";
            //         let doubleCardUrl = BASE_DOUBLE_CARD_URL + leftCardId + "/" + rightCardId + ".png";
            //         doubleCardNameHTML += doubleCardName + "<br />";
            //         doubleCardUrlHTML += doubleCardUrl + "<br />";
            //         console.log(doubleCardName);
            //     }
            //
            //     if (leftCardIdolizedId === data["unit"]["rank_max_card_id"]) {
            //         let doubleCardName = rightCardIdolizedId + "~~~SIF合卡-" + leftId + "_" + rightId + "-" + team + "-" + leftIdolName + "_" + rightIdolName
            //             + "-" + cardType + "-" + collection + "-" + rarity + "-觉醒";
            //         let doubleCardUrl = BASE_DOUBLE_CARD_URL + leftCardIdolizedId + "/" + rightCardIdolizedId + ".png";
            //         doubleCardNameHTML += doubleCardName + "<br />";
            //         doubleCardUrlHTML += doubleCardUrl + "<br />";
            //         console.log(doubleCardName);
            //     }
            // }

            // 下面为双人卡
            if (data["multi_unit"] !== false) {
                if (dFlag) {
                    dFlag = false;
                    let dUrl = BASE_CARD_TRANS_URL + data["multi_unit"]["still_asset"];
                    let dName = dUrl.substring(dUrl.lastIndexOf('/') + 1, dUrl.lastIndexOf('.')) + "~~~SIF双人卡-" +
                        data["multi_unit"]["unit_list"][0] + '_' + data["multi_unit"]["unit_list"][1] + "-" + team + "-" + dPreIdolName + "_" + idolName;
                    cardUrlHTML += dUrl + "<br />";
                    cardNameHTML += dName + "<br />";
                    console.log(dName);
                } else {
                    dFlag = true
                    dPreIdolName = idolName
                }




            }

        }
    }
    xml.send();
}


